package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Api(tags = "员工接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(), // 加密秘钥
                jwtProperties.getAdminTtl(), // token过期时间
                claims);

        // 设置需要返回给接口的数据
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出登录")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @ApiOperation("新增员工")
    @PostMapping
    public Result<String> add(@RequestBody EmployeeDTO employee) {
        log.info("新增员工: {}", employee);

        employeeService.add(employee);

        return Result.success("新增成功");
    }

    @ApiOperation("修改员工")
    @PutMapping
    public Result<String> edit(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return Result.success("修改成功");
    }

    @ApiOperation("获取员工详情")
    @GetMapping("/{id}")
    public Result<Employee> get(@PathVariable Integer id) {
        return Result.success(employeeService.getById(id));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<Page<Employee>> page(EmployeePageQueryDTO employeePageQueryDTO) {
        Page<Employee> data = employeeService.page(employeePageQueryDTO);
        return Result.success(data);
    }

    @ApiOperation("启动或禁用员工")
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable Integer status, Integer id) {
        employeeService.status(status, id);
        return Result.success("操作成功");
    }
}
